$(document).ready(function(){
	var loadSyncShellConsole = $("#loadSyncShellConsole");
	loadSyncShellConsole.load("shell_console.html?taskName=sync-log-from-hdfs-to-elastic", function(response, status, xhr) {
		loadSyncShellConsole.html(response);
	});
	// 开始时间选择器
	$( "#startTime" ).datetimepicker({
        timeFormat: "HH:mm:ss",
        dateFormat: "yy-mm-dd",
        showOtherMonths: true,
      	selectOtherMonths: true,
      	changeMonth: true,
      	changeYear: true,
      	showButtonPanel: true
    });

	// 结束时间选择器
	$( "#endTime" ).datetimepicker({
        timeFormat: "HH:mm:ss",
        dateFormat: "yy-mm-dd",
        showOtherMonths: true,
      	selectOtherMonths: true,
      	changeMonth: true,
      	changeYear: true,
      	showButtonPanel: true
    });
	
	$("#start_sync_log").click(function() {
		var startTime = $("#startTime").prop('value');
		var endTime = $("#endTime").prop('value');
		validate(startTime, endTime);
		var param = {
			startTime:startTime,
			endTime:endTime
		};
		new HttpUtils().post("com.lvtu.dashboard.ssh.submitSyncLogJob", param, function(data){
			alert("提交任务成功");
			loadSyncShellConsole.empty();
			loadSyncShellConsole.load("shell_console.html?taskName=sync-log-from-hdfs-to-elastic", function(response, status, xhr) {
				loadSyncShellConsole.html(response);
			});
		});
	});
	
	function validate(startTime, endTime) {
		var reg = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/;
		if(startTime || startTime.trim() != '') {
			if(!reg.test(startTime)) {
				return false;
			}
		}
		if(endTime || endTime.trim() != '') {
			if(!reg.test(endTime)) {
				return false;
			}
		}
		var startTimeDate = getDate(startTime);
		var endTimeDate = getDate(endTime);
		if(endTime == null || endTimeDate == null) {
			return true;
		}
		if(startTimeDate > endTimeDate) {
			return false;
		}
		return true;
	}
	
	function getDate(dateStr) {
		var reg = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/;
		if(!reg.test(dateStr)) {
			return null;
		}
		dateStr = dateStr.replace(/-/g,'/');
		return new Date(dateStr);
	}
});